#include "gofgame/dungeonkeeper/ui/StrategicDataView.h"
#include "gofgame/common/new.h"
#include "gofgame/common/graphics/LinearGradientBrush.h"
#include "gofgame/common/graphics/HornedTextureBrush.h"
#include "gofgame/common/ui/TabPage.h"
#include "gofgame/common/ui/DockLayout.h"
#include "gofgame/common/ui/DockStyle.h"
#include "gofgame/common/ui/Transforms.h"
#include "gofgame/dungeonkeeper/ui/HeroView.h"
#include "gofgame/dungeonkeeper/ui/UnitView.h"
#include "gofgame/dungeonkeeper/ui/ProduceView.h"
#include "gofgame/dungeonkeeper/ui/ItemView.h"

using namespace gofgame::dungeonkeeper::ui;
using namespace gofgame::common::ui;
using namespace gofgame::common;

HornedTextureBrushResource StrategicDataView::selectedHeroTabResource(27, 24, "gofgame/dungeonkeeper/ui/hero_tab.png");
HornedTextureBrushResource StrategicDataView::heroTabResource(27, 24, "gofgame/dungeonkeeper/ui/hero_tab_gray.png");
HornedTextureBrushResource StrategicDataView::selectedUnitTabResource(27, 24, "gofgame/dungeonkeeper/ui/unit_tab.png");
HornedTextureBrushResource StrategicDataView::unitTabResource(27, 24, "gofgame/dungeonkeeper/ui/unit_tab_gray.png");
HornedTextureBrushResource StrategicDataView::selectedProduceTabResource(27, 24, "gofgame/dungeonkeeper/ui/produce_tab.png");
HornedTextureBrushResource StrategicDataView::produceTabResource(27, 24, "gofgame/dungeonkeeper/ui/produce_tab_gray.png");
HornedTextureBrushResource StrategicDataView::selectedItemTabResource(27, 24, "gofgame/dungeonkeeper/ui/item_tab.png");
HornedTextureBrushResource StrategicDataView::itemTabResource(27, 24, "gofgame/dungeonkeeper/ui/item_tab_gray.png");
TextureBrushResource StrategicDataView::tabCardBackResource("gofgame/dungeonkeeper/ui/tab_card_back.png");
TextureBrushResource StrategicDataView::tabBaseLineResource("gofgame/dungeonkeeper/ui/tab_base_line.png");

bool StrategicDataView::init() {
	if (!TabControl::init()) {
		return false;
	}

	this->setCardDistance(
		heroTabResource.unwrap()->getTexture()->getContentSize().height);
	this->setCardDockStyle(DockStyle::DockStyle_Top);
	TabPage *tabPage;

	Ref<LinearGradientBrush> brush;
	c_assign(brush, new LinearGradientBrush(0.5F, 0.5F, 0.5F, 0.5F, 0, 0, 0, 0.5F));

	tabPage = nullptr;
	oc_new<TabPage>(tabPage);
	tabPage->autorelease();
	tabPage->setLayout(new DockLayout());
	tabPage->setText("Hero");
	tabPage->setPadding(10);
	tabPage->setBackgroundBrush(brush);
	this->addChild(tabPage);
	this->heroView.initialize();
	this->heroView->init();
	tabPage->addChild(this->heroView);

	tabPage = nullptr;
	oc_new<TabPage>(tabPage);
	tabPage->autorelease();
	tabPage->setLayout(new DockLayout());
	tabPage->setText("Unit");
	tabPage->setPadding(10);
	tabPage->setBackgroundBrush(brush);
	this->addChild(tabPage);
	this->unitView.initialize();
	this->unitView->init();
	tabPage->addChild(this->unitView);

	tabPage = nullptr;
	oc_new<TabPage>(tabPage);
	tabPage->autorelease();
	tabPage->setLayout(new DockLayout());
	tabPage->setText("Produce");
	tabPage->setPadding(10);
	tabPage->setBackgroundBrush(brush);
	this->addChild(tabPage);
	this->produceView.initialize();
	this->produceView->init();
	tabPage->addChild(this->produceView);

	tabPage = nullptr;
	oc_new<TabPage>(tabPage);
	tabPage->autorelease();
	tabPage->setLayout(new DockLayout());
	tabPage->setText("Item");
	this->addChild(tabPage);
	tabPage->setPadding(10);
	tabPage->setBackgroundBrush(brush);
	this->itemView.initialize();
	this->itemView->init();
	tabPage->addChild(this->itemView);

	/*Ref<Hero> hero;
	hero.initialize();*/
	HeroViewInfo hi = Hero::getHeroViewInfo(1);
	this->heroView->setHeroViewInfo(hi);
	return true;
}

void StrategicDataView::drawCardsBackground() {
	CCRect rect = Transforms::uiRectToCocos2dRect(this, this->getCardsBound());
	tabCardBackResource.unwrap()->fillRect(rect);
}

void StrategicDataView::drawCardsBaseLine() {
	if (this->getCardDockStyle() != DockStyle_Top) {
		_throw(IllegalStateException, "dockStyle must be top");
	}
	int height = tabBaseLineResource.unwrap()->getTexture()->getContentSize().height;
	CCRect rect = Transforms::uiRectToCocos2dRect(this, this->getCardsBound(height));
	rect.origin.y -= 0;
	tabBaseLineResource.unwrap()->fillRect(rect);
}

void StrategicDataView::drawTabButton(TabPage *tabPage, const CCRect& tabButtonRect) {
	CCRect rect = Transforms::uiRectToCocos2dRect(this, tabButtonRect);
	if (tabPage == this->heroView->getParent()) {
		if (tabPage->isSelected()) {
			this->selectedHeroTabResource.unwrap()->fillRect(rect);
		} else {
			this->heroTabResource.unwrap()->fillRect(rect);
		}
	} else if (tabPage == this->unitView->getParent()) {
		if (tabPage->isSelected()) {
			this->selectedUnitTabResource.unwrap()->fillRect(rect);
		} else {
			this->unitTabResource.unwrap()->fillRect(rect);
		}
	} else if (tabPage == this->produceView->getParent()) {
		if (tabPage->isSelected()) {
			this->selectedProduceTabResource.unwrap()->fillRect(rect);
		} else {
			this->produceTabResource.unwrap()->fillRect(rect);
		}
	} else if (tabPage == this->itemView->getParent()) {
		if (tabPage->isSelected()) {
			this->selectedItemTabResource.unwrap()->fillRect(rect);
		} else {
			this->itemTabResource.unwrap()->fillRect(rect);
		}
	} else {
		TabControl::drawTabButton(tabPage, tabButtonRect);
	}
}
